package role

import (
	"sanguo/node/node_map"
	"sanguo/node/node_map/scene"
	ss_rpc "sanguo/protocol/ss/rpc"
	"sanguo/rpc/leaveMap"

	"github.com/golang/protobuf/proto"
)

type LeaveMap struct {
}

func (this *LeaveMap) OnCall(replyer *leaveMap.LeaveMapReplyer, arg *ss_rpc.LeaveMapReq) {

	node_map.Debugln("OnCall LeaveMap", arg)

	scene_ := scene.GetSceneByType(scene.SceneType(arg.GetMapType()))

	if nil == scene_ {
		replyer.Reply(&ss_rpc.LeaveMapResp{
			Ok: proto.Int32(0),
		})
		return
	}

	scene_.PostTask(func() {

		node_map.Debugln("do leave map")

		sceneO := scene_.GetObjByID(arg.GetUserID())
		if nil == sceneO {
			replyer.Reply(&ss_rpc.LeaveMapResp{
				Ok: proto.Int32(1),
			})
			return
		}

		if scene_.LeaveScene(sceneO) {
			sceneO.OnLeaveScene()
			replyer.Reply(&ss_rpc.LeaveMapResp{
				Ok: proto.Int32(1),
			})
		} else {
			replyer.Reply(&ss_rpc.LeaveMapResp{
				Ok: proto.Int32(0),
			})
		}

		node_map.Debugln(sceneO.(*Role).GetID(), "do leave map ok")
	})
}

func init() {
	leaveMap.Register(&LeaveMap{})
}
